﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStorage
{
    public class Repository
    {
        const string filenameBookmark = "bookmark_";
        const string filenameTag = "tag_";

        const string fileext = ".bin";

        public string CreateFilename(Guid Guid)
        {
            return filenameBookmark + Guid + fileext;
        }

        public void UpdateBookmarkLocal(Bookmark b)
        { 
            SqlRepository r = new SqlRepository();
            r.UpdateBookmark(b);
        }

        public void DeleteBookmarkLocal(Bookmark b)
        {
            //var b = new Bookmark { Id = sGuid, Name = title, Description = desc, Url = url, CreatedDate = DateTime.Now };

            SqlRepository r = new SqlRepository();
            r.DeleteBookmark(b);
        }

        public void DeleteBookmarkCloud(Bookmark b)
        {
            SqlRepository r = new SqlRepository();
            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.DeleteFromStorage<Bookmark>(CreateFilename(b.Id), StorageFolder.bookmarks, b);
            }
        }

        public void DeleteTagLocal(Tag t)
        {
            SqlRepository r = new SqlRepository();
            r.DeleteTag(t);
        }

        public void DeleteTagCloud(Tag t)
        {
            SqlRepository r = new SqlRepository();
            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.DeleteFromStorage<Tag>(CreateFilename(t.Id), StorageFolder.tags, t);
            }
        }

        public void DeleteListLocal(Lst l)
        {
            SqlRepository r = new SqlRepository();
            r.DeleteList(l);
        }

        public void DeleteListCloud(Lst l)
        {
            SqlRepository r = new SqlRepository();
            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.DeleteFromStorage<Lst>(CreateFilename(l.Id), StorageFolder.lists, l);
            }
        }

        public bool IsAuthenticated()
        {
            bool isAuth = false;

            try
            {
                var sqlRepository = new SqlRepository();
                isAuth = sqlRepository.GetSync().IsAuthenticated;
            }
            catch (Exception)
            {
                    
            }
            
            return isAuth;
        }

        public User GetUser()
        {
            SqlRepository r = new SqlRepository();
            return r.GetUser();
        }

        public void UpdateListLocalCloud(Lst l)
        {
            SqlRepository r = new SqlRepository();
            bool isOffline = r.IsOffline();
            r.UpdateList(l);

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                DeleteListCloud(l);
                CloudRepository.SaveToStorage<Lst>(CreateFilename(l.Id), StorageFolder.lists, l);
            }
        }

        public void UpdateTagLocalCloud(Tag t)
        {
            SqlRepository r = new SqlRepository();
            bool isOffline = r.IsOffline();
            r.UpdateTag(t);

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                DeleteTagCloud(t);
                CloudRepository.SaveToStorage<Tag>(CreateFilename(t.Id), StorageFolder.tags, t);
            }
        }

        public void UpdateBookmarkLocalCloud(Bookmark b)
        { 
            SqlRepository r = new SqlRepository();  
            bool isOffline = r.IsOffline();
            r.UpdateBookmark(b);

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                DeleteBookmarkCloud(b);
                CloudRepository.SaveToStorage<Bookmark>(CreateFilename(b.Id), StorageFolder.bookmarks, b);
            }
        }

        public Guid InsertBookmarkLocalCloud(string title, string url, string desc)
        {
            Guid sGuid = Guid.NewGuid();

            var b = new Bookmark { Id = sGuid, Name = title, Description = desc, Url = url, CreatedDate = DateTime.Now };

            SqlRepository r = new SqlRepository();
            r.InsertBookmark(b);

            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.SaveToStorage<Bookmark>(CreateFilename(sGuid), StorageFolder.bookmarks, b);
            }

            return sGuid;
        }

        public Guid InsertTagLocalCloud(string name)
        {
            Guid sGuid = Guid.NewGuid();

            var t = new Tag { Id = sGuid, Name = name, CreatedDate = DateTime.Now };

            SqlRepository r = new SqlRepository();
            r.InsertTag(t);

            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.SaveToStorage<Tag>(CreateFilename(sGuid), StorageFolder.tags, t);
            }

            return sGuid;
        }

        public Guid InsertListLocalCloud(string name)
        {
            Guid sGuid = Guid.NewGuid();

            var l = new Lst { Id = sGuid, Name = name, CreatedDate = DateTime.Now };

            SqlRepository r = new SqlRepository();
            r.InsertList(l);

            bool isOffline = r.IsOffline();

            //Check if we are offline, if offline don't save to cloud storage
            if (!isOffline)
            {
                CloudRepository.SaveToStorage<Lst>(CreateFilename(sGuid), StorageFolder.lists, l);
            }

            return sGuid;
        }

        public void SyncData()
        { 
            
        }

        public Bookmark GetBookmark(Guid guid)
        {
            return new SqlRepository().GetBookmark(guid);
        }

        public List<Bookmark> GetBookmarks()
        {
            List<Bookmark> list = new List<Bookmark>();

            SqlRepository r = new SqlRepository();

            if (r.IsOffline() && r.GetSync().LastSynced.HasValue)
            {
                list = r.GetAllBookmarks();
            }
            
            if (list.Count == 0)
            {
                list = CloudRepository.GetAllBookmarkItems(StorageFolder.bookmarks.ToString());

                //TODO 
                //Put the bookmarks into sqlite 
                r.InsertBookmarkBatch(list);
            }

            return list; 
        }

        public List<Tag> GetTags()
        {
            List<Tag> list = new List<Tag>();

            SqlRepository r = new SqlRepository();

            if (r.IsOffline() && r.GetSync().LastSynced.HasValue)
            {
                list = r.GetAllTags();
            }

            if (list.Count == 0)
            {
                list = CloudRepository.GetAllTagItems(StorageFolder.tags.ToString());

                //TODO 
                //Put the bookmarks into sqlite 
                r.InsertTagBatch(list);
            }

            return list;
        }

        public void SigninBooky(string username, string password)
        {
            SqlRepository r = new SqlRepository();
            r.CreateDatabaseTables();
            r.SaveDefaultStorageSync();
            r.InsertUser(new User() { Id = 1, Username = username, Password = password });
            CloudRepository.CreateCloudFolders();
        }

        public void SignoutBooky()
        {
            SqlRepository r = new SqlRepository();
            r.DropDatabaseTables();
            CloudRepository.RemoveSecurityToken();
        }

        //public bool IsAuthenticated()
        //{
        //    SqlRepository r = new SqlRepository();
        //    return r.GetSync().IsAuthenticated;
        //}



        //public static Bookmark LoadBookmark(Guid id)
        //{
        //    //Person person = LoadFromStorage<Person>(fileName);
        //    var b = new Bookmark { Id = id };
        //    b = SerializationHelper.LoadFromStorage<Bookmark>(filename);
        //    return b;
        //}

    }
}
